<template>
    <el-container>
        <!--    头部区    -->
        <el-header>
            <div>
                <img src="../assets/logo.png">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="quit">退出</el-button>
        </el-header>
        <!--    主体区    -->
        <el-container>
            <!--   侧边栏   -->
            <el-aside :width="isCollapse?'64px':'200px'">
                <!--      侧边栏展开效果          -->
                <div class="toggle-button" @click="menuToggle">|||</div>
                <!--     包裹菜单        -->
                <el-menu
                        :default-active="activePath"
                        background-color="#545c64"
                        text-color="#fff"
                        active-text-color="#6495ED"
                        unique-opened
                        router
                        :collapse-transition="false"
                        :collapse="isCollapse"
                >
                    <!--       一级菜单        -->
                    <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="objIcon[item.id]"></i>
                            <span class="el-name">{{item.authName}}</span>
                        </template>
                    <!--     二级菜单       -->
                        <el-menu-item :index="'/'+item1.path" v-for="item1 in item.children" :key="item1.id" @click="saveActiveState('/'+item1.path)">
                            <template slot="title" >
                                <i class="el-icon-menu"></i>
                                <span>{{item1.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!--    右面内容区    -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        name: "home",
        created() {
            this.getMenuList()
            this.activePath=window.sessionStorage.getItem('activePath')
        },
        data(){
            return{
                menuList:[],
                objIcon:{
                    "125":"iconfont icon-icon-",
                    "103":"iconfont icon-quanxian",
                    "101":"iconfont icon-icon-shangpin",
                    "102":"iconfont icon-icon_danju",
                    "145":"iconfont icon-baobiao"
                },
                isCollapse:false,
                activePath:""
            }
        },
        methods:{
            quit(){
                //清除sessionStorage
                window.sessionStorage.clear()
                //跳转到登录页
                this.$router.push('/login')
            },
            //获取菜单数据
            async getMenuList(){
                const {data}=await this.$http.get('menus')
                if (data.meta.status!==200)return this.$message.error(data.meta.msg)
                this.menuList=data.data
            },
            //隐藏表单
            menuToggle(){
                this.isCollapse=!this.isCollapse
            },
            saveActiveState(text){
                console.log(text)
                window.sessionStorage.setItem('activePath',text)
                this.activePath=text
            }
        }
    }
</script>

<style lang="less" scoped>
    .el-container{
        height: 100%;
    }
    .el-header{
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fff;
        font-size: 18px;
        div{
            display: flex;
            align-items: center;
            img{
                width: 50px;
                height: 50px;
            }
            span{
                margin-left: 15px;
            }
        }
    }
    .el-aside{
        background-color: #333744;
        overflow: hidden;
        .el-menu{
            border-right: none;
        }
    }
    .el-main{
        background-color: #eaedf1;
    }
    .el-name{
        margin-left: 15px;
    }
    .toggle-button{
        background-color: #4a5064;
        color: #fff;
        font-size: 10px;
        line-height: 24px;
        text-align: center;
        letter-spacing: 0.3em;
    }
</style>